package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.echoesnet.echoesshortvideo.Api.ApiException;
import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.Beans.VideoDetailEntity;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Repositories.TinyVideoInfoRepository;
import com.echoesnet.echoesshortvideo.Repositories.VideoDetailRepository;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责驱动UI
 *
 * @author lc
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:05
 * @description
 */
public class VideoDetailViewModel extends ViewModel
{
    private MutableLiveData<VideoDetailEntity> videoDetail = new MutableLiveData<>();
    private MutableLiveData<VideoDetailEntity> moreVideoDetail = new MutableLiveData<>();
    private MutableLiveData<String> likeResult = new MutableLiveData();
    private MutableLiveData<String> commentResult = new MutableLiveData();
    private MutableLiveData<String> shareVideo = new MutableLiveData();
    private MutableLiveData<String> focusResult = new MutableLiveData();
    private MutableLiveData<Integer> deleteVideo = new MutableLiveData();


    private VideoDetailRepository uDetailRepository;

    public LiveData<String> getLikeData()
    {
        return likeResult;
    }

    public LiveData<VideoDetailEntity> getVideoDetail()
    {
        return videoDetail;
    }

    public LiveData<VideoDetailEntity> getMoreVideoDetail()
    {
        return moreVideoDetail;
    }
    public LiveData<String> getCommentDetail()
    {
        return commentResult;
    }

    public MutableLiveData<String> getShareVideo()
    {
        return shareVideo;
    }
    public LiveData<String> getFocusResult()
    {
        return focusResult;
    }
    public MutableLiveData<Integer> getDeleteVideo()
    {
        return deleteVideo;
    }


    private TinyVideoInfoRepository tinyVideoInfoRepository;

    public VideoDetailViewModel()
    {
        this.uDetailRepository = new VideoDetailRepository();
        this.tinyVideoInfoRepository = new TinyVideoInfoRepository();
    }

    public void startGetVideoDetail(String videoId, String openType, String toUserId, String getDataType)
    {
        uDetailRepository.getVideoDetail(videoId, openType, toUserId, getDataType, new SilenceSubscriber<VideoDetailEntity>()
        {
            @Override
            public void onNext(VideoDetailEntity response)
            {
                super.onNext(response);
                videoDetail.setValue(response);
            }
        });
    }

    public void startGetMoreVideoDetail(String videoId, String openType, String toUserId, String getDataType)
    {
        uDetailRepository.getMoreVideoDetail(videoId, openType, toUserId, getDataType, new SilenceSubscriber<VideoDetailEntity>()
        {
            @Override
            public void onNext(VideoDetailEntity response)
            {
                super.onNext(response);
                moreVideoDetail.setValue(response);
            }
        });
    }

    /**
     * 点赞视频
     *
     * @param videoId 视频ID
     */
    public void startLikeVideo(String videoId)
    {
        uDetailRepository.likeVideo(Api.VIDEOC_LIKEVIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                likeResult.setValue("0");
            }

            @Override
            public void onHandledError(ApiException apiE)
            {
                super.onHandledError(apiE);
            }
        });
    }

    /**
     * 评论视频
     *
     * @param videoId        视频ID
     * @param commentContent 评论内容
     */
    public void startCommentVideo(String videoId, String commentContent)
    {
        uDetailRepository.commentVideo(Api.VIDEOC_COMMENT_VIDEO, videoId, commentContent, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                commentResult.setValue("0");
            }
        });
    }

    /**
     * 分享视频
     *
     * @param videoId 用户ID
     */
    public void shareVideo(String videoId)
    {
        uDetailRepository.shareVideo(Api.VIDEOC_SHARE_VIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                shareVideo.setValue("0");
            }
        });
    }

    /**
     * 关注用户
     *
     * @param toUserId 用户ID
     */
    public void focusPerson(String toUserId)
    {
        uDetailRepository.focus(Api.USERC_CHECK_FOLLOW, toUserId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                focusResult.setValue("0");
            }
        });
    }

    /**
     * 删除视频
     *
     * @param videoId 用户ID
     */
    public void deleteVideo(int position,String videoId)
    {
        uDetailRepository.deleteVideo(Api.VIDEOC_DELETE_VIDEO, videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                deleteVideo.setValue(position);
                tinyVideoInfoRepository.deleteVideoInfo(videoId);
            }
        });
    }



    public MutableLiveData<Map<String, String>> checkVideoState(String videoId)
    {
        MutableLiveData<Map<String, String>> checkVideoResult = new MutableLiveData<>();

        tinyVideoInfoRepository.checkVideo(videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject object = new JSONObject(response);
                    String result = object.getString("delStatus");
                    Map<String, String> map = new HashMap();
                    map.put("videoId", videoId);
                    map.put("result", result);
                    checkVideoResult.setValue(map);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

        return checkVideoResult;
    }
}
